package zouran.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zouran.entity.Hospital;
import zouran.exception.MyRunTimeException;
import zouran.result.ErrorMessage;
import zouran.result.ResultMap;
import zouran.service.EarthquakeInfoService;
import zouran.service.HospitalService;

/**
 * Hospital Api
 * Author：zouran
 * Date：2023/3/31  21:48
 * Description:
 */
@RestController
public class HospitalController {
    private final HospitalService hospitalService;
    private final ResultMap<Hospital> resultMap;
    private final EarthquakeInfoService earthquakeInfoService;

    public HospitalController(HospitalService hospitalService, ResultMap<Hospital> resultMap, EarthquakeInfoService earthquakeInfoService) {
        this.hospitalService = hospitalService;
        this.resultMap = resultMap;
        this.earthquakeInfoService = earthquakeInfoService;
    }

    /**
     * 全部医院
     */
    @RequestMapping("/getAllHospital")
    public ResultMap getAllHospital() {
        return resultMap.success(hospitalService.getAllHospital());
    }


    /**
     * 医院id查医院
     *
     * @param hospitalId 医院编号
     * @return 医院信息
     */
    @RequestMapping("/getHospitalByHospitalId")
    public ResultMap getHospitalByHospitalId(@RequestParam("hospitalId") long hospitalId) {
        return resultMap.success(hospitalService.getHospitalByHospitalId(hospitalId));
    }


    /**
     * 坐标附近医院
     *
     * @param lon         经度
     * @param lat         纬度
     * @param lonDistance 长轴
     * @param latDistance 短轴
     * @return 医院信息列表
     */
    @RequestMapping("/getAllNearHospitalByPosition")
    public ResultMap getAllNearHospitalByPosition(@RequestParam("longitude") Double lon, @RequestParam("latitude") Double lat, @RequestParam("lonDistance") Double lonDistance, @RequestParam("latDistance") Double latDistance) {
        return resultMap.success(hospitalService.getAllNearHospitalByPosition(lon, lat, lonDistance, latDistance));
    }

    /**
     * 坐标附近医院坐标
     *
     * @param lon         经度
     * @param lat         纬度
     * @param lonDistance 长轴
     * @param latDistance 短轴
     * @return 医院位置信息列表
     */
    @RequestMapping("/getAllNearHospitalPositionByPosition")
    public ResultMap getAllNearHospitalPositionByPosition(@RequestParam("longitude") Double lon, @RequestParam("latitude") Double lat, @RequestParam("lonDistance") Double lonDistance, @RequestParam("latDistance") Double latDistance) {
        return resultMap.success(hospitalService.getAllNearHospitalPositionByPosition(lon, lat, lonDistance, latDistance));
    }


    /**
     * 地震id查医院坐标
     *
     * @param earthquakeId 地震编号
     * @return 附件医院位置信息列表
     */
    @RequestMapping("/getAllNearHospitalPositionByEarthquakeId")
    public ResultMap getAllNearHospitalPositionByEarthquakeId(@RequestParam("earthquakeId") long earthquakeId) {
        if (!earthquakeInfoService.judgeEarthquakeInfoExistById(earthquakeId))
            throw new MyRunTimeException(ErrorMessage.EARTHQUAKE_ID_ERROR.getMessage());
        return resultMap.success(hospitalService.getAllNearHospitalPositionByEarthquakeId(earthquakeId));
    }


    /**
     * 地震id查医院
     *
     * @param earthquakeId 地震编号
     * @return 附件医院信息列表
     */
    @RequestMapping("/getAllNearHospitalByEarthquakeId")
    public ResultMap getAllNearHospitalByEarthquakeId(@RequestParam("earthquakeId") long earthquakeId) {
        if (!earthquakeInfoService.judgeEarthquakeInfoExistById(earthquakeId))
            throw new MyRunTimeException(ErrorMessage.EARTHQUAKE_ID_ERROR.getMessage());
        return resultMap.success(hospitalService.getAllNearHospitalByEarthquakeId(earthquakeId));
    }


    /**
     * 查坐标附近最近医院
     *
     * @param lon 经度
     * @param lat 纬度
     * @return 最近的一个医院信息
     */
    @RequestMapping("/getNearestHospitalByPosition")
    public ResultMap getNearestHospitalByPosition(@RequestParam("longitude") double lon, @RequestParam("latitude") double lat) {
        return resultMap.success(hospitalService.getNearestHospitalByPosition(lon, lat));
    }

}
